package com.xiaoxin.project.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxin.framework.aspectj.lang.enums.DataSourceType;
import com.xiaoxin.framework.datasource.DynamicDataSourceContextHolder;
import com.xiaoxin.project.system.domain.SysConfig;
import com.xiaoxin.project.system.service.SysConfigService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;


/**
 * 广播表测试
 * @author ZhangXX
 * @version 1.0
 * @data 2021/5/18 22:49
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test5")
public class test5 {
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 广播表测试，会随机找一个库去查询
     */
    @Test
    public void testBroadcastTables1() {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SHARDING.name());
        List<SysConfig> sysConfigs = sysConfigService.list(new QueryWrapper<>(new SysConfig()));
        System.out.println(sysConfigs);
        DynamicDataSourceContextHolder.clearDataSourceType();
    }

    /**
     * 广播表测试增删改，会随机找一个库去 不配置  会随机找一个库插入
     */
    @Test
    public void testBroadcastTables2() {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SHARDING.name());
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigKey("config1");
        sysConfig.setConfigName("配置1");
        sysConfigService.save(sysConfig);
        DynamicDataSourceContextHolder.clearDataSourceType();
    }

    @Test
    public void testBroadcastTables3() {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SHARDING.name());
        sysConfigService.removeById(8);
        DynamicDataSourceContextHolder.clearDataSourceType();
    }

    @Test
    public void testBroadcastTables4() {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SHARDING.name());
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigId(6);
        sysConfig.setConfigKey("config2");
        sysConfig.setConfigName("配置2");
        sysConfigService.updateById(sysConfig);
        DynamicDataSourceContextHolder.clearDataSourceType();
    }
}
